package com.maidshop.animation.controller;

import com.maidshop.animation.domain.Animation;
import com.maidshop.animation.domain.dto.AnimationDto;
import com.maidshop.animation.domain.vo.AnimationVo;
import com.maidshop.animation.service.AnimationService;
import com.maidshop.common.core.controller.BaseController;
import com.maidshop.common.core.page.TableDataInfo;
import com.maidshop.elasticsearch.domain.AnimationEs;
import com.maidshop.elasticsearch.service.ElasticsearchService;
import com.maidshop.elasticsearch.service.impl.ElasticsearchImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/animation")
public class AnimationController extends BaseController {

    @Resource
    private AnimationService animationService;

    @Resource
    private ElasticsearchService elasticsearchService;

    @GetMapping("/list")
    public TableDataInfo list(AnimationVo vo) {
        startPage();
        List<AnimationDto> list = animationService.selectList(vo);
        return getDataTable(list);
    }

    @GetMapping("/listByES")
    public List<AnimationEs> listByES(@RequestParam(value = "text") String text) {
        startPage();
        List<AnimationEs> list = elasticsearchService.findByNameOrTypeOrIntro(text);
        return list;
    }

    @PostMapping("/insertOrUpdate")
    public int insert(@RequestBody Animation animation) {
        if (animation.getId() == null) {
            return animationService.insertNewAnimation(animation);
        }
        return animationService.updateAnimation(animation);
    }

    @GetMapping("/getOneAnimation/{id}")
    public AnimationDto getOneAnimation(@PathVariable("id") Long id) {
        return animationService.selectOne(id);
    }

    @PostMapping("/delete/{id}")
    public int delete(@PathVariable("id") Long id) {
        return animationService.deleteAnimation(id);
    }

}
